<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class ProductController extends Controller{

	//定义一个属性
	private $model;
	//构造函数
	public function __init()
	{		
			$this->model=new \Common\Model\Product;	
		parent::__init();
	}
	
    public function index(){
    	 //获得商品gid
    	$gid=Q('get.gid',0,'intval');
		View::with('gid',$gid);
    	//获得货品属性的信息
    	$goodlistdata=$this->model->where('goods_gid='.$gid)->get();		
		foreach ($goodlistdata as $k => $v) {
			$goodlistdata[$k]['connectid']=explode(',', $v['connectid']);
		}

/*这里是没处理过connectid的$goodlistdata;
 * Array
(
    [0] => Array
        (
            [plid] => 16
            [connectid] => Array
                (
                    [0] => 10
                    [1] => 12
                    [2] => 13
                )

            [plstock] => 11
            [plorder] => 111
            [goods_gid] => 3
        )

    [1] => Array
        (
            [plid] => 17
            [connectid] => Array
                (
                    [0] => 10
                    [1] => 12
                    [2] => 0
                )

            [plstock] => 11
            [plorder] => 111
            [goods_gid] => 3
        )

)
*/		
		
		//获得表中connectid对应的属性名称
		$gaModel=new \Common\Model\Goodsattr;
		//获得所有的商品的属性
		$gadata=$gaModel->get();
		foreach ($goodlistdata as $k => $v) {
			foreach ($v['connectid'] as $kk => $vv) {
				foreach ($gadata as $key => $value) {
					if($value['gaid']==$vv){
						$goodlistdata[$k]['connectid'][$kk]=$value['gavalue'];
					}
				}
			}
		}
/****************************************************************************
 * 这里是处理过connectid的goodlist的数组，也就是说讲connectid里面的键值（也就是商品属性表里的id），通过重组数组来是把对应
 * 主键id的gavalue的名字拿到并且压入到connect中，把之前的gaid数字换成gavalue的内容
 * Array
(
    [0] => Array
        (
            [plid] => 16
            [connectid] => Array
                (
                    [0] => 11111
                    [1] => 2323
                    [2] => xxl
                )

            [plstock] => 11
            [plorder] => 111
            [goods_gid] => 3
        )

    [1] => Array
        (
            [plid] => 17
            [connectid] => Array
                (
                    [0] => 11111
                    [1] => 2323
                    [2] => 0
                )

            [plstock] => 11
            [plorder] => 111
            [goods_gid] => 3
        )

)
 * 
 * ***************************************************************************
*/		
		View::with('goodlistdata',$goodlistdata);		

		//通过对类型属性表和商品属性表的关联来获得规格的属性。冰爷
		$goodsattrModel=new \Common\Model\Goodsattr;
		$data=$goodsattrModel->join('typeattr','typeattr_taid','=','taid')->where("goods_gid={$gid} and tatype=1")->get();
//		p($data);exit;
		if(empty($data)){
			View::error('该商品没有规格');
		}
	
		//重新作何这个关联的数组定义一个空数组,按照taid作为2维 的键名，然偶再循环数组压入到对应的3维霞
//	如下图所示	
/****************************************************************************************************************
没有从组之前的数组
 *Array
(
    [0] => Array
        (
            [gaid] => 10
            [gavalue] => 11111
            [gaadd] => 1212
            [goods_gid] => 3
            [typeattr_taid] => 1
            [taid] => 1
            [taname] => 111111111111111111
            [tavalue] => 11111
            [tatype] => 1
            [form_fid] => 1
        )

    [1] => Array
        (
            [gaid] => 11
            [gavalue] => 11111
            [gaadd] => 1
            [goods_gid] => 3
            [typeattr_taid] => 1
            [taid] => 1
            [taname] => 111111111111111111
            [tavalue] => 11111
            [tatype] => 1
            [form_fid] => 1
        )

    [2] => Array
        (
            [gaid] => 12
            [gavalue] => 2323
            [gaadd] => 0
            [goods_gid] => 3
            [typeattr_taid] => 3
            [taid] => 3
            [taname] => 大幅度是
            [tavalue] => 122,2323,2323,2323,
            [tatype] => 1
            [form_fid] => 1
        )

    [3] => Array
        (
            [gaid] => 13
            [gavalue] => xxl
            [gaadd] => 0
            [goods_gid] => 3
            [typeattr_taid] => 4
            [taid] => 4
            [taname] => 磁场
            [tavalue] => x,xl,xxl,
            [tatype] => 1
            [form_fid] => 1
        )
 * 声明一个空数组来接收从组的的数据
 * 	$arr=array();
 * 循环上面的数组按照taid作为2维数组的键名进行从组，这样的话对应的taid会放在同一个二维键名下	//（这是一类数组从组的思路：当时一维数组的时候，想吗）
		foreach ($data as $k => $v) {		
				$arr[$v['taid']]=$v;								
		}
		View::with('data',$data);
        View::make();
    }
 * Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [gaid] => 10
                    [gavalue] => 11111
                    [gaadd] => 1212
                    [goods_gid] => 3
                    [typeattr_taid] => 1
                    [taid] => 1
                    [taname] => 111111111111111111
                    [tavalue] => 11111
                    [tatype] => 1
                    [form_fid] => 1
                )

            [1] => Array
                (
                    [gaid] => 11
                    [gavalue] => 11111
                    [gaadd] => 1
                    [goods_gid] => 3
                    [typeattr_taid] => 1
                    [taid] => 1
                    [taname] => 111111111111111111
                    [tavalue] => 11111
                    [tatype] => 1
                    [form_fid] => 1
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [gaid] => 12
                    [gavalue] => 2323
                    [gaadd] => 0
                    [goods_gid] => 3
                    [typeattr_taid] => 3
                    [taid] => 3
                    [taname] => 大幅度是
                    [tavalue] => 122,2323,2323,2323,
                    [tatype] => 1
                    [form_fid] => 1
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [gaid] => 13
                    [gavalue] => xxl
                    [gaadd] => 0
                    [goods_gid] => 3
                    [typeattr_taid] => 4
                    [taid] => 4
                    [taname] => 磁场
                    [tavalue] => x,xl,xxl,
                    [tatype] => 1
                    [form_fid] => 1
                )

        )

)

*****************************************************************************************/
	$arr=array();	
		foreach ($data as $k => $v) {		
				$arr[$v['taid']][]=$v;								
		}				
		View::with('arr',$arr);
        View::make();
    }

	//异步ajax的请求来实现对数据库的添加
	public function ajaxadd(){
			
		//到模型出来所有步奏
		if($this->model->ajaxmodel()){
			//获得商品列表的所数据
		
			$file=$this->model->where("connectid='".implode(',', Q('post.spec'))."'")->find();
			$file['connectid']=explode(',', $file['connectid']);
		//这里得处理connectid将里面的的对应商品的主键换成对应的内容
			//获得表中connectid对应的属性名称
			$gaModel=new \Common\Model\Goodsattr;
		//获得所有的商品的属性
		
			$gadata=$gaModel->get();
			foreach ($file['connectid'] as $k=> $v) {
				foreach ($gadata as $ke=> $va) {
					if($va['gaid']==$v){
						$file['connectid'][$k]=$va['gavalue'];
					}
				}
			}
			
			echo json_encode($file);
		}else{
			echo json_encode(FALSE);
		}
		
		
	}
	//异步ajax来删除对应的货品表
	public function ajaxdel(){
	  //获得post提交上啦的plid值
	  $plid=Q('post.plid'); 
//	  删除对应的数据	
		$this->model->where('plid='.$plid)->delete();
	//删除成功提示
		echo json_encode(true);
	}
	
	
	//修改数据
	public function edit(){
		//判断是不是有提交
		if(IS_POST){
			if($this->model->edit())return View::success('修改成功',U('product/index',array('gid'=>Q('get.gid',0,'intval'))));
			View::error($this->model->geterror());
		}
		
		
		    	 //获得商品gid		    	 
    	$gid=Q('get.gid',0,'intval');	
		//通过对类型属性表和商品属性表的关联来获得规格的属性。冰爷
		$goodsattrModel=new \Common\Model\Goodsattr;
		$data=$goodsattrModel->join('typeattr','typeattr_taid','=','taid')->where("goods_gid={$gid} and tatype=1")->get();
		$arr=array();	
		foreach ($data as $k => $v) {		
				$arr[$v['taid']][]=$v;								
		}	
		//获得对应plid的货品属性的信息
 		
    	$goodlistdata=$this->model->where('plid='.Q('get.plid',0,'intval'))->find();
		//将获得数据
		
		$goodlistdata['connectid']=explode(',', $goodlistdata['connectid']);		
		View::with('goodlistdata',$goodlistdata);	
		
		View::with('arr',$arr);
	    View::make();
	}
		
}
